package com.qfedu.controller;

import com.qfedu.model.Item;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.IItemService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/7/3 0:00
 */

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * @Author Tbh.
     * @Description: 子项工程分页
     * @Date 2022/7/3 1:03
     * @Param QueryPageBean
     * @return PageResult
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = itemService.findPage(queryPageBean);
        return result;

    }

    /**
     * @Author Tbh.
     * @Description: 增加子项工程
     * @Date 2022/7/3 1:03
     * @Param Item
     * @return Result
     */
    @PostMapping("add")
    public Result addItem(@RequestBody Item item) {
        int row = itemService.addItem(item);
        if (row > 0) {
            //枚举类名.对象名.get方法 获取到对应的属性值
            //Status.SUCCESS.name();  获取到枚举对象名  SUCCESS
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * @Author Tbh.
     * @Description: 数据回显
     * @Date 2022/7/3 1:04
     * @Param iid
     * @return Result
     */
    @GetMapping("findByIid")
    public Result findByPid(@RequestParam("iid") String iid) {
        Item item = itemService.findByIid(iid);
        Result result = new Result();
        result.setMessage("fail");
        if (ObjectUtils.isNotEmpty(item)) {
            result.setFlag(true);
            result.setData(item);
            result.setMessage("success");
        }
        return result;

    }

    /**
     * @Author Tbh.
     * @Description: 修改子项工程
     * @Date 2022/7/3 1:04
     * @Param Item
     * @return Result
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Item item) {
        int row = itemService.edit(item);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * @Author Tbh.
     * @Description: 逻辑删除
     * @Date 2022/7/3 1:05
     * @Param iid
     * @return Result
     */
    @GetMapping("delete")
    public Result delete(@RequestParam("iid") String iid){
        int row =  itemService.deleteByIid(iid);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
